package com.metel.mservice.common.jaxrs.interceptor;

import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.logging.FaultListener;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;

/**
 * PreInvokeServiceInterceptor
 * 
 * @author sr80.choi
 */
public class PreInvokeServiceInterceptor extends AbstractPhaseInterceptor<Message> {

	public PreInvokeServiceInterceptor() {
		super(Phase.PRE_INVOKE);
	}

	@Override
	public void handleMessage(Message message) throws Fault {
		// set FaultListener
		Object property = message.getContextualProperty("org.apache.cxf.logging.FaultListener");
		if (property == null || !(property instanceof FaultListener)) {
			FaultListener faultListener = getFaultListener();

			message.setContextualProperty("org.apache.cxf.logging.FaultListener", faultListener);
		}
	}

	private FaultListener getFaultListener() {
		FaultListener listener = new FaultListener() {
			@Override
			public boolean faultOccurred(Exception exception, String description, Message message) {
				return false;
			}
		};

		return listener;
	}
}
